// pages/securitiescentre/securitiescentre.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shoplist:'',
  },
  getlist:function(){
    ajax('/api/CouponCollection/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      Vtype:0,
      ActivityIDStr: '',
      PageIndex: 0,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district,
      Title:''
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        // console.log(res)
        this.setData({
          shoplist: res.Models
        })
      })
  },
  //立即领取
  lingqu:function(e){
    // console.log(e)
    var id = e.currentTarget.dataset.id
    ajax('/api/UploadReceiveCouponPrimaryNew/UploadAdd?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      UserCouponID: id,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district,
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "post",
      }, res => {
        // console.log(res)
        wx.showToast({
          title: '领取成功',
          icon: 'none'
        })
      })
    this.getlist()
  },
  // 分享领取
  fenxiang:function(e){
    var rid = e.currentTarget.dataset.rid;
    var id = e.currentTarget.dataset.id
    if (rid == 0){
      ajax('/api//UploadReceiveCouponPrimary/UploadAdd?requestContent=' + JSON.stringify({
        token: wx.getStorageSync('Access_token'),
        UserCouponID: id,
        ProvinceName: wx.getStorageSync('address_component').province,
        CityName: wx.getStorageSync('address_component').city,
        DistrictName: wx.getStorageSync('address_component').district,
      }), {
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "post",
        }, res => {
          // console.log(res)
          
          var id = e.currentTarget.dataset.id;
          var index = e.currentTarget.dataset.index;
          var rid = res.Status
          console.log(rid)
          wx.navigateTo({
            url: '/pages/unpacking-securities/securities?id=' + id + '&rid=' + rid,
          })
        })
    }else{
      // console.log(rid, 123)
      // for(var a in )
      var id = e.currentTarget.dataset.id;
      var rid = e.currentTarget.dataset.rid
      wx.navigateTo({
        url: '/pages/unpacking-securities/securities?id=' + id + '&rid=' + rid,
      })
    }
    
  },
  // 立即使用跳转
  tiaozhuan:function(e){
    console.log(e)

    var pid = e.currentTarget.dataset.pid
    var vid = e.currentTarget.dataset.vid

    var id = e.currentTarget.dataset.id    //发布方ID
    var aid = e.currentTarget.dataset.aid   //活动ID

    if(vid == 0){
      wx.navigateTo({
        url: '/pages/index/index',
      })
    } else if (vid == 3) {  //商品详情  传id   
      wx.navigateTo({
        url: '/pages/supermarket-details/details?ID=' + aid + '&shopID=' + id,
      })
    }  else if (vid == 2) { //跳转商户 传商户id  PublisherID
      wx.navigateTo({
        url: '/pages/shop-homepage/homepage?ID=' + id,
      })
    }



    else if (vid == 1) { //拼购详情 传id   ActivityID
      // wx.navigateTo({
      //   url: '/pages/group-purchase-commodity/commodity?ID=' + pid,
      // })
      wx.showToast({
        title: '拼购暂未开启',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    this.getlist()
  },
  wheninput:function(){
    wx.navigateTo({
      url: '/pages/searchcoupon/searchcoupon',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})